package com.ktwlsoft.archivemanage.domain.business.businessService;

import com.ktwlsoft.archivemanage.domain.business.repositoryService.*;
import com.ktwlsoft.archivemanage.domain.dao.entity.OperationLog;
import com.ktwlsoft.archivemanage.domain.dao.entity.Role;
import com.ktwlsoft.archivemanage.payload.exception.BusinessException;
import com.ktwlsoft.archivemanage.payload.operationLog.SaveLogRequest;
import com.ktwlsoft.archivemanage.payload.role.EditRoleRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 角色操作相关
 */
@Service
public class RoleService {

    private final RoleRepositoryService roleRepositoryService;

    private final RoleActionRepositoryService roleActionRepositoryService;

    private final RoleMenuRepositoryService roleMenuRepositoryService;

    private final UserRolesRepositoryService userRolesRepositoryService;

    private final AccountRepositoryService accountRepositoryService;


    public RoleService(RoleRepositoryService roleRepositoryService, RoleActionRepositoryService roleActionRepositoryService, RoleMenuRepositoryService roleMenuRepositoryService, UserRolesRepositoryService userRolesRepositoryService, AccountRepositoryService accountRepositoryService) {
        this.roleRepositoryService = roleRepositoryService;
        this.roleActionRepositoryService = roleActionRepositoryService;
        this.roleMenuRepositoryService = roleMenuRepositoryService;
        this.userRolesRepositoryService = userRolesRepositoryService;
        this.accountRepositoryService = accountRepositoryService;
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void updateRole(EditRoleRequest request){
        roleRepositoryService.update(request);
        roleActionRepositoryService.updateRoleInfo(request);
        roleMenuRepositoryService.updateRoleInfo(request);
        userRolesRepositoryService.updateRoleInfo(request);
        accountRepositoryService.updateRoleInfo(request);
        OperationRepositoryService.saveLog(new SaveLogRequest(
                OperationLog.OperationType.EDIT,
                "编辑角色《"+ request.getRoleName() +"》",
                "角色管理",
                OperationLog.BusinessType.SYSTEM));
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void deleteRole(Long roleId){
        if(roleActionRepositoryService.existsByRoleId(roleId)){
            throw new BusinessException("角色行为存在绑定，不能删除！");
        }
        if(roleMenuRepositoryService.existsByRoleId(roleId)){
            throw new BusinessException("角色菜单存在绑定，不能删除！");
        }
        if(userRolesRepositoryService.existsByROleId(roleId)){
            throw new BusinessException("用户角色存在绑定，不能删除！");
        }
        Role role = roleRepositoryService.getById(roleId);
        if(role != null){
            OperationRepositoryService.saveLog(new SaveLogRequest(
                    OperationLog.OperationType.DELETE,
                    "删除角色《"+ role.getRoleName() +"》",
                    "角色管理",
                    OperationLog.BusinessType.SYSTEM));
        }
        roleRepositoryService.deleteById(roleId);
    }
}
